﻿using IBll;
using IDal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Model;
using System;

namespace Bll
{
    public class HoomBll : IHoomBll
    {
        public HoomBll(IBaseDal<Hoom>hoomDal,IBaseDal<HoomType>hoomtypeDal)
        {
            HoomDal = hoomDal;
            HoomtypeDal = hoomtypeDal;
        }

        public IBaseDal<Hoom> HoomDal { get; }
        public IBaseDal<HoomType> HoomtypeDal { get; }

        public async Task<int> AddHoom(Hoom hoom)
        {
            var weiyi = HoomDal.GetAsync().Any(x => x.HoomNumber == hoom.HoomNumber);
            if (!weiyi)
            {
                return await HoomDal.AddAsync(hoom);    
            }
            else
            {
                return -2;
            }
        }
        public List<HoomType> GetHoomType()
        {
            return HoomtypeDal.GetAsync().ToList();
        }
        public dynamic GetHoom(string? hoomnumber, int typeId)
        {
            var hoom = HoomDal.GetAsync();
            var hoomtype=HoomtypeDal.GetAsync();
            var list = from a in hoom
                       join b in hoomtype
                       on a.TypeId equals b.TypeId
                       select new
                       {
                           a.Id,
                           a.HoomNumber,
                           a.TypeId,
                           a.Massgae,
                           a.People,
                           b.TypeName
                       };
            if (!string.IsNullOrEmpty(hoomnumber))
            {
                list = list.Where(x => x.HoomNumber.Contains(hoomnumber));
            }
            if (typeId > 0)
            {
                list=list.Where(x => x.TypeId == typeId);
            }
            return list.ToList();
        }
        public Hoom FanTian(int id)
        {
            return HoomDal.GetAsync().FirstOrDefault(x => x.Id == id);    
        }
        public async Task<int> UpdaHoom(Hoom hoom)
        {
            var weiyi = HoomDal.GetAsync().Any(x => x.HoomNumber == hoom.HoomNumber && x.Id == hoom.Id);
            if (!weiyi)
            {
                return await HoomDal.UpdaAsync(hoom);
            }
            else
            {
                return -2;
            }
        }
    }
}
